package com.yuanluludev.app.activity

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.model.BitmapDescriptorFactory
import com.amap.api.maps2d.model.LatLng
import com.amap.api.maps2d.model.MarkerOptions
import com.amap.api.maps2d.model.PolylineOptions
import com.yuanluludev.app.R
import com.yuanluludev.gaode.utils.LocUtils
import com.yuanluludev.gaode.service.LocationService
import com.yuanluludev.lib_base.base.BaseActivity
import com.yuanluludev.lib_utils.util.*
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.android.synthetic.main.activity_gaode_map.*
import java.util.concurrent.TimeUnit

class GaoDeMapActivity : BaseActivity() {

    private var timerSubscribe: Disposable? = null
    private var num = 0

    override fun getLayoutResId(): Int {
        return R.layout.activity_gaode_map
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun initEvents() {
        LocUtils.requestLocPermissionAndLoc(this)
//        Putils.checkPermission(this, backgroundLocPermissions, 1000)
//        ActivityCompat.requestPermissions(this,
//            backgroundLocPermissions, 1)
        periodUpdate()

//        tvNum?.setOnClickListener {
//            show()
//        }

    }

    private fun show() {
        AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage("由于安卓系统限制，请您手动开启本软件需要的所有权限，从而保障此软件能正常运行。")
            .setCancelable(false)
            .setNegativeButton(
                "下次再说"
            ) { dialog, which ->

            }
            .setPositiveButton(
                "立即设置"
            ) { dialog, which ->
                AppUtils.launchAppDetailsSettings()
            }.show()
    }

    override fun initEvents(savedInstanceState: Bundle?) {
        mapView?.onCreate(savedInstanceState)
    }

    private fun periodUpdate() {
        destroyTimer()
        timerSubscribe = Observable.interval(0, 3, TimeUnit.SECONDS).subscribe {
            ThreadUtils.runOnUiThread {
                setCurrentLoc()
                num++
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setCurrentLoc() {

        val currentLocInfo = LocationService.latestLocation?.toString()?.replace("#", "\n")
//        //定位信息
//        LogUtils.e(currentLocInfo)
//        //定位时间
//        LogUtils.e(TimeUtils.millis2String(LocationService.latestLocation?.time?:0L))
        tvCurrentLocInfo?.text =currentLocInfo
        tvNum?.text =
            "$num -- ${ServiceUtils.isServiceRunning(LocationService::class.java)} "
    }

    private fun destroyTimer() {
        timerSubscribe?.let {
            if (!it.isDisposed) it.dispose()
            timerSubscribe = null
        }
    }

    override fun onResume() {
        super.onResume()
        LocUtils.judgeLocPermissionAndLoc()
        mapView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView?.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView?.onSaveInstanceState(outState)
    }

    override fun onDestroy() {
        destroyTimer()
        mapView?.onDestroy()
        super.onDestroy()
    }

    /**
     * 创建marker
     */
    private fun createMarker(stationName: String, stationSrc: Int, latLng: LatLng) {
//        ViewUtils.layoutId2View(R.layout.main_marker_view)?.apply {
//            findViewById<TextView?>(R.id.tvStationName)?.text = stationName
//            findViewById<ImageView?>(R.id.ivStation)?.imageResource = stationSrc
//            val marker = BitmapDescriptorFactory.fromView(this)
//            mapView?.map?.addMarker(MarkerOptions().position(latLng).icon(marker))
//        }
    }

//    mapView?.map?.apply {
//        //将地图移动到定位点
//        moveCamera(CameraUpdateFactory.changeLatLng(latLngs[latLngs.size - 1]))
//        //起点位置和 地图界面大小控制
//        moveCamera(CameraUpdateFactory.newLatLngZoom(latLngs[latLngs.size - 1], 12.0F))
//        //添加轨迹
//        addPolyline(
//            PolylineOptions()
//                .addAll(latLngs)
//                .width(10f)
//                .color(
//                    ColorUtils.getColor(R.color.colorPrimary)
//                ).visible(true)
//        )
//    }

}